package repository

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
)

type RepoLocalCompConvert struct {
}

func (e *RepoLocalCompConvert) ConvertCloud2Local(cloud map[string]interface{}) (map[string]interface{}, error) {
	// 提取业务参数
	modelType := Map.GetString(cloud, "modelType", "")
	modelName := Map.GetString(cloud, "modelName", "")
	manufacturer := Map.GetString(cloud, "manufacturer", "")
	deviceType := Map.GetString(cloud, "deviceType", "")
	compId := Map.GetString(cloud, "compId", "")

	// 必选参数
	if Method.HasEmpty(modelType, modelName) {
		return nil, errors.New("缺少参数： modelType, modelName")
	}

	localMap := make(map[string]interface{})

	if modelType == "decoder" {
		localMap["compRepo"] = "local"
		localMap["compType"] = "jar-decoder"
		localMap["compParam"] = make(map[string]interface{})

		// 必选参数
		if Method.HasEmpty(modelName, manufacturer, deviceType) {
			return nil, errors.New("缺少参数： modelName, manufacturer, deviceType")
		}

		fileName := modelName + ".jar"

		// 可选参数
		compParam := Map.GetMap(localMap, "compParam", nil)
		compParam["manufacturer"] = manufacturer
		compParam["deviceType"] = deviceType
		compParam["compId"] = compId
		compParam["modelName"] = modelName
		compParam["fileName"] = fileName

		return localMap, nil
	}

	return localMap, nil
}

func (e *RepoLocalCompConvert) ConvertConf2Local(confMap map[string]interface{}) (map[string]interface{}, error) {
	localMap := make(map[string]interface{})
	localMap["compRepo"] = "local"
	localMap["compType"] = "app-service"
	localMap["compParam"] = make(map[string]interface{})

	// 提取业务参数
	appName := Map.GetString(confMap, "appName", "")
	appType := Map.GetString(confMap, "appType", "")
	if Method.HasEmpty(appType, appName) {
		return nil, errors.New("参数不能为空: appName,appTyp")
	}

	// 可选参数
	compParam := Map.GetMap(localMap, "compParam", nil)
	compParam["appEngine"] = confMap["appEngine"]
	compParam["appName"] = confMap["appName"]
	compParam["appType"] = confMap["appType"]
	compParam["fileName"] = confMap["fileName"]
	compParam["loaderName"] = confMap["loaderName"]
	compParam["confFiles"] = confMap["confFiles"]
	compParam["userParam"] = confMap["userParam"]

	return localMap, nil
}

func (e *RepoLocalCompConvert) ConvertFileName2Local(fileName string) (map[string]interface{}, error) {
	localMap := make(map[string]interface{})
	localMap["compRepo"] = "local"
	localMap["compType"] = "jar-decoder"
	localMap["compParam"] = make(map[string]interface{})

	// 提取业务参数
	manufacturer := "Fox-Edge"
	deviceType := "public"
	if Method.HasEmpty(fileName) {
		return nil, errors.New("文件名称不能为空")
	}

	// 从结构化的文件名中，取出信息
	modelName := LocalJarFileName.GetModelName(fileName)
	if Method.HasEmpty(modelName) {
		return nil, errors.New("参数不能为空：modelName")
	}

	// 可选参数
	compParam := Map.GetMap(localMap, "compParam", nil)
	compParam["manufacturer"] = manufacturer
	compParam["deviceType"] = deviceType
	compParam["modelName"] = modelName
	compParam["fileName"] = fileName

	return localMap, nil
}

func (e *RepoLocalCompConvert) ConvertOperateEntity2Local(operateEntity *edgeEntity.OperateMethodEntity) (map[string]interface{}, error) {
	localMap := make(map[string]interface{})
	localMap["compRepo"] = "local"
	localMap["compType"] = "jar-decoder"
	localMap["compParam"] = make(map[string]interface{})

	fileName := Map.GetString(operateEntity.EngineParam, "file", "")

	// 从结构化的文件名中，取出信息
	modelName := LocalJarFileName.GetModelName(fileName)
	if Method.HasEmpty(modelName) {
		return nil, errors.New("参数不能为空：modelName")
	}

	// 可选参数
	compParam := Map.GetMap(localMap, "compParam", nil)
	compParam["manufacturer"] = operateEntity.Manufacturer
	compParam["deviceType"] = operateEntity.DeviceType
	compParam["modelName"] = modelName
	compParam["fileName"] = fileName

	return localMap, nil
}
